package ch3.Singleton;

/**
 * 基于静态内部类的单例模式
 * getInstance()被调用的时候，JVM会初始化这个方法所访问的内部静态类InstanceHolder，使得InstanceHolder的静态变量INSTANCE被初始化，
 * 从而使StaticHolderSingleton类的唯一实例得以创建
 * 由于类的静态变量只会创建一次，因此StaticHolderSingleton只会被创建一次
 */

public class StaticHolderSingleton {
    private StaticHolderSingleton() {
        System.out.println("StaticHolderSingleton inited.");
    }

    private static class InstanceHolder {
        final static StaticHolderSingleton INSTANCE = new StaticHolderSingleton();
    }

    public static StaticHolderSingleton getInstance() {
        System.out.println("getInstance invoked.");
        return InstanceHolder.INSTANCE;
    }

    public void someService() {
        System.out.println("do something");
    }

    public static void main(String[] args) {
        StaticHolderSingleton.getInstance().someService();
    }

}
